/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.netbrain.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import mx.netbrain.dao.TreeDao;
import mx.netbrain.dao.impl.TreeDaoImpl;
import mx.netbrain.model.Status;
import mx.netbrain.model.Tree;
import mx.netbrain.model.User;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author Julian
 */
public class FileUploadUtil {

    public static void uploadValidate(UploadedFile file, String destination, Long root, User user) {
        String extValidate;
        if (file != null) {
            String ext = file.getFileName();
            if (ext != null) {
                extValidate = ext.substring(ext.indexOf(".") + 1);
            } else {
                extValidate = null;
            }
            if (extValidate.equals("png")) {
                try {
                    transferFile(file.getFileName(), file.getInputstream(), file, destination);
                } catch (IOException e) {
                    FacesContext context = FacesContext.getCurrentInstance();
                    context.addMessage(null, new FacesMessage("Wrong", "Error al cargar archivo."));
                }
                String nombre = file.getFileName();
                String delimit = "[.]";
                String[] tokens = nombre.split(delimit);
                System.out.println(tokens[0]);
                Tree tree = new Tree();
                Status status = new Status();
                TreeDao treeDao = new TreeDaoImpl();
                status.setId(1);
                if (tokens.length > 0) {
                    tree.setName(tokens[0]);
                    tree.setExtension(tokens[1]);
                } else {
                    tree.setName(tokens[0]);
                }
                tree.setRoot(root);
                tree.setRoute(destination);
                tree.setSize(file.getSize() + "");
                tree.setType("Documento");
                tree.setRegister(new Date());
                treeDao.save(tree);
                FacesContext context = FacesContext.getCurrentInstance();
                context.addMessage(null, new FacesMessage("Succefull", file.getFileName() + " se cargo correctamente!"));
            } else {
                FacesContext context = FacesContext.getCurrentInstance();
                context.addMessage(null, new FacesMessage("Wrong", "Solo extensiones PNG."));
            }
        } else {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage("Wrong", "Seleccione un archivo."));
        }
    }

    public static void transferFile(String fileName, InputStream in, UploadedFile file, String destination) {
        try {
            OutputStream out = new FileOutputStream(new File(destination + fileName));
            int reader = 0;
            byte[] bytes = new byte[(int) file.getSize()];
            while ((reader = in.read(bytes)) != -1) {
                out.write(bytes, 0, reader);
            }
            in.close();
            out.flush();
            out.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    public static void editFile(Tree treeEdit) {
        Tree tree = new Tree();
        TreeDao treeDao = new TreeDaoImpl();
        tree = treeDao.findById(treeEdit.getId());
        File antiguo = new File(tree.getRoute() + tree.getName() + "." + tree.getExtension());
        File nuevo = new File(treeEdit.getRoute() + treeEdit.getName() + "." + tree.getExtension());
        treeDao.update(treeEdit);
        antiguo.renameTo(nuevo);
    }
}
